package com.aioz.security.service.impl;

import com.aioz.security.common.ResponseResult;
import com.aioz.security.entity.Application;
import com.aioz.security.entity.Interview;
import com.aioz.security.entity.LoginUser;
import com.aioz.security.entity.Resume;
import com.aioz.security.entity.dto.InterviewDto;
import com.aioz.security.mapper.ApplicationMapper;
import com.aioz.security.mapper.InterviewMapper;
import com.aioz.security.mapper.ResumeMapper;
import com.aioz.security.service.InterviewService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Aioz
 * @since 2022-09-27
 */
@Service
@Slf4j
public class InterviewServiceImpl extends ServiceImpl<InterviewMapper, Interview> implements InterviewService {

    @Resource
    private InterviewMapper interviewMapper;

    @Resource
    private ResumeMapper resumeMapper;

    @Resource
    private ApplicationServiceImpl applicationService;

    @Override
    public ResponseResult inviteInterview(Interview interview,Integer applicationId) {
        Application application = applicationService.getById(applicationId);
        Interview isExist = query().eq("position_id", application.getPositionId())
                .eq("user_id", application.getUserId())
                .one();
        if(null != isExist){
            String msg = "您以邀请过该用户！";
            return new ResponseResult<>(200,msg);
        }
        application.setState(3);
        applicationService.updateById(application);
        interview.setResumeId(application.getResumeId());
        interview.setCompanyId(application.getCompanyId());
        interview.setHrId(application.getHrId());
        interview.setUserId(application.getUserId());
        interview.setPositionId(application.getPositionId());
        interview.setStatus(1);
        interviewMapper.insert(interview);
        String msg = "面试邀请成功!";
        return new ResponseResult(200,msg);
    }

    @Override
    public ResponseResult checkInterview() {
        QueryWrapper<Interview> wrapperInterview = new QueryWrapper<>();
        wrapperInterview.eq("user_id",getCurrentUserId());
        List<Interview> interviews = interviewMapper.selectList(wrapperInterview);
        return new ResponseResult(200,interviews);
    }

    @Override
    public ResponseResult approveInterview(Interview interview) {
        interviewMapper.updateById(interview);
        return new ResponseResult(200,"面试审批成功!");
    }

    @Override
    public ResponseResult getResultInterview() {
        QueryWrapper<Interview> wrapperInterview = new QueryWrapper<>();
        wrapperInterview.eq("user_id",getCurrentUserId());
        List<Interview> interviews = interviewMapper.selectList(wrapperInterview);
        interviews = interviews.stream().filter(interview -> interview.getStatus() != 0).collect(Collectors.toList());
        return new ResponseResult(200,interviews);
    }

    @Override
    public ResponseResult getAllInterViews() {
        List<InterviewDto> interviews = interviewMapper.queryAllInterviews();
        for (InterviewDto interview : interviews) {
            interview.setResume(resumeMapper.selectById(interview.getResumeId()));
        }
        return new ResponseResult(200,interviews);
    }

    @Override
    public ResponseResult checkSpecificInterview(Integer applicationID) {
        //获取登录用户Id
        Long userId = getCurrentUserId();
        log.info("applicationID:{}",applicationID);
        log.info("userId:       {}",userId);
        Integer positionId = applicationService.getById(applicationID).getPositionId();
        log.info("positionId:   {}",positionId);
        Interview interview = query().eq("user_id", userId).eq("position_id", positionId).one();
        log.info("interview:    {}",interview);
        return new ResponseResult<>(200,interview);
    }

    public Long getCurrentUserId(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        return loginUser.getUser().getId();
    }
}
